<?php 
/**
*销售数据分析
*/
require('./include/init.php');

$address=$_REQUEST['address'];
//连接数据库
$mysql=mysql::getIns();
date_default_timezone_set('Asia/Shanghai');
//今天的时间戳
$t = time();
$start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
$end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));

//统计今天销售区人数
$sql="select count(*) from alldev where timestamp > $start and timestamp < $end and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$nowservice=$mysql->getOne($sql);

//昨天的时间戳
$t = time();
$start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t))-24*3600;
$end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t))-24*3600;

//昨天销售区人数
// $sql="select * from alldev where timestamp > $start and timestamp < $end and area='销售区'";
$sql="select count(*) from alldev where timestamp > $start and timestamp < $end and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$yesdservice=$mysql->getOne($sql);

// print_r($yesdservice);
// exit;

//获取本周周一的时间
$one=strtotime('-1 monday', time());
// echo $one . '<br />';
// echo date('Y-m-d H:i:s',$one);
// exit;
//获取本周周二时间
$two=strtotime('-1 monday', time())+24*3600*1;

//获取本周周三时间
$three=strtotime('-1 monday', time())+24*3600*2;

//获取本周周四时间
$four=strtotime('-1 monday', time())+24*3600*3;

//获取本周周五时间
$five=strtotime('-1 monday', time())+24*3600*4;

//获取本周周六时间
$six=strtotime('-1 monday', time())+24*3600*5;

//获取本周周日时间
$seven=strtotime('-1 monday', time())+24*3600*6;
// echo date('Y-m-d H:i:s',$seven);

//获取本周周一
$nowone=strtotime('-1 monday', time())+24*3600*7;
// echo date('Y-m-d H:i:s',$nowone);


//统计本周周一进店量
$sql="select count(*) from alldev where timestamp >= $one and timestamp < $two and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$onejk=$mysql->getOne($sql);
//统计本周周二进店量
$sql="select count(*) from alldev where timestamp >= $two and timestamp < $three and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$twojk=$mysql->getOne($sql);
//统计本周周三进店量
$sql="select count(*) from alldev where timestamp >= $three and timestamp < $four and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$threejk=$mysql->getOne($sql);
//统计本周周四进店量
$sql="select count(*) from alldev where timestamp >= $four and timestamp < $five and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$fourjk=$mysql->getOne($sql);
//统计本周周五进店量
$sql="select count(*) from alldev where timestamp >= $five and timestamp < $six and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$fivejk=$mysql->getOne($sql);
// echo $fivejk;
// exit;
//统计本周周六进店量
$sql="select count(*) from alldev where timestamp >= $six and timestamp < $seven and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
// $sql="select * from alldev where timestamp >= $six and timestamp < $seven and shop='". $address ."'";
$sixjk=$mysql->getOne($sql);
// echo $sixjk;
// exit;
//统计本周周日进店量
$sql="select count(*) from alldev where timestamp >= $seven and timestamp < $nowone and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$sevenjk=$mysql->getOne($sql);




/**************************
获取本月第一个周一的时间

**********************/
$year=date('Y',time());
$month=date('m',time());
$base_time = strtotime ( "{$year}-{$month}-01" );
$base_w = date ( "w", $base_time );

if($base_w == '1'){
  //刚好是星期一
  $first = $base_time;
}else{
  //不是星期一
  //找到上个星期天是几月几号
  $last_sun = date ( "Y-m-d", strtotime("-$base_w day", $base_time));
  //在上个星期天的基础上 加8天
  $first = strtotime ( "+8 day", strtotime ( $last_sun ) );
}
//第二个周一的时间
$second=$first+7*24*3600;
// echo date('Y-m-d H:i:s',$second);
// exit;
//第三个时间戳
$third=$second+7*24*3600;
// echo date('Y-m-d H:i:s',$third);
// exit;
//第四个时间戳
$fourth=$third+7*24*3600;
// echo date('Y-m-d H:i:s',$fourth);
// exit;
//下周一时间戳
$fifth=$fourth+7*24*3600;
// echo date('Y-m-d H:i:s',$third);
// exit;


//获取这个月第五周时间
$fifth =mktime(0,0,0,date('m',time()),1,date('Y',time()));
// echo date('Y-m-d H:i:s',$fifth);
// exit;


//获取这个月第一周的人数
$sql="select count(*) from alldev where timestamp >= $first and timestamp < $second and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$firstjk=$mysql->getOne($sql);
// echo $firstjk;
// exit;

//获取这个月第二周的人数
$sql="select count(*) from alldev where timestamp >= $second and timestamp < $third and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$secondjk=$mysql->getOne($sql);
// echo $secondjk;
// exit;
$bak=$secondjk;
//获取这个月第三周的人数
$sql="select count(*) from alldev where timestamp >= $third and timestamp < $fourth and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$thirdjk=$mysql->getOne($sql);
// echo $thirdjk;
// exit;

//获取这个月第四周的人数
$sql="select count(*) from alldev where timestamp >= $fourth and timestamp < $fifth and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$fourthjk=$mysql->getOne($sql);




//获取第一季度开始时间戳
$firstmonth =mktime(0,0,0,1,1,date('Y',time()));

//获取第二季度开始时间
$secondmonth =mktime(0,0,0,4,1,date('Y',time()));
// echo date('Y-m-d H:i:s',$secondmonth);

//获取第三季度开始时间
$thirdmonth =mktime(0,0,0,7,1,date('Y',time()));
// echo date('Y-m-d H:i:s',$thirdmonth);

//获取第四季度开始时间
$fourthmonth =mktime(0,0,0,10,1,date('Y',time()));
// echo date('Y-m-d H:i:s',$fourthmonth);

//获取明年第一季度开始时间
$firstmonths =mktime(0,0,0,1,1,date('Y',time())+1);
// echo date('Y-m-d H:i:s',$firstmonths);



//获取第一季度的人数
$sql="select count(*) from alldev where timestamp >= $firstmonth and timestamp < $secondmonth and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$firstmonth=$mysql->getOne($sql);
// echo $firstmonth;
// exit;

//获取第二季度的人数
$sql="select count(*) from alldev where timestamp >= $secondmonth and timestamp < $thirdmonth and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$secondmonth=$mysql->getOne($sql);

//获取第三季度的人数
$sql="select count(*) from alldev where timestamp >= $thirdmonth and timestamp < $fourthmonth and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$thirdmonth=$mysql->getOne($sql);
// echo $thirdmonth;
// exit;


//获取第四季度的人数
$sql="select count(*) from alldev where timestamp >= $fourthmonth and timestamp < $firstmonths and shop='". $address ."' and area in ('展厅区','销售区','精品区')";
$fourthmonth=$mysql->getOne($sql);

// echo $fourthmonth;
// exit;

/*************************************

	进店频次(每天统计一次,今天累计人数的进店频次)


************************************/
//统计当天累计人数数
$t = time();
$start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
$end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
$sql="select count(*) from alldev where timestamp > $start and timestamp < $end and shop='". $address ."'";
$allday=$mysql->getOne($sql);
// echo $allday;
// exit;
//统计进店频次为2次的
$sql="select count(*) from alldev where timestamp > $start and timestamp < $end  and visittimes='2' and shop='". $address ."'";
$visittwo=$mysql->getOne($sql);
// echo $visittwo;
// exit;

//统计进店频次为3次
$sql="select count(*) from alldev where timestamp > $start and timestamp < $end  and visittimes='3' and shop='". $address ."'";
$visitthree=$mysql->getOne($sql);
// echo $visitthree;
// exit;



//统计进店频次为3次以上
$sql="select count(*) from alldev where timestamp > $start and timestamp < $end  and visittimes > '3' and shop='". $address ."'";
$visitmore=$mysql->getOne($sql);
// echo $visitmore;
// exit;

//计算频次

//两次
$vtwo=ceil(($visittwo/$allday)*100);
// echo $vtwo;
// exit;
//三次
$vthree=ceil(($visitthree/$allday)*100);
// echo $vthree;
// exit;

//三次以上
$vmore=ceil(($visitmore/$allday)*100);
// echo $vmore;
// exit;

//一次
$vone=100-$vtwo-$vthree-$vmore;
// echo $vone;
// exit;








/*************************************


			虚拟数据



************************************/

/*****************************************************


			虚拟数据


***************************************************/


if($address=='北京博瑞祥星汽车销售有限公司'){
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=443;
		$secondjk=580;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=3382;
		$secondmonth=2192;
		$thirdmonth+=1023;
		$thirdmonth-=$bak;
	}
}else if($address=='义乌市博奥汽车销售服务有限公司'){
	$ts=time();
	if($one<1469158943){
		$onejk=121;
	}
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=462;
		$secondjk=510;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=2519;
		$secondmonth=3751;
		$thirdmonth+=972;
		$thirdmonth-=$bak;
	}
}else if($address=='广州粤奥汽车销售服务有限公司'){
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=460;
		$secondjk=492;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=2435;
		$secondmonth=1840;
		$thirdmonth+=952;
		$thirdmonth-=$bak;
	}
}else if($address=='辽宁鑫迪汽车销售服务有限公司'){
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=800;
		$secondjk=297;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=10323;
		$secondmonth=16254;
		$thirdmonth+=1097;
		$thirdmonth-=$bak;
	}
}else if($address=='江苏天奥汽车销售服务有限公司'){
	$ts=time();
	if($one<1469158943){
		$onejk=54;
	}
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=226;
		$secondjk=483;
		$thirdjk+=54;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=2509;
		$secondmonth=3155;
		$thirdmonth+=763;
		$thirdmonth-=$bak;
	}
}else if($address=='成都新元素雅麓汽车销售服务有限公司'){
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=378;
		$secondjk=391;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=3459;
		$secondmonth=3602;
		$thirdmonth+=769;
		$thirdmonth-=$bak;
	}
}else if($address=='云南驰泰汽车销售服务有限公司'){
	$ts=time();
	if($one<1469158943){
		$onejk=75;
	}
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=445;
		$secondjk=577;
		$thirdjk+=75;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=8420;
		$secondmonth=10198;
		$thirdmonth+=1097;
		$thirdmonth-=$bak;
	}
}else if($address=='宁夏奥立升汽车销售服务有限公司'){
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=340;
		$secondjk=512;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=4876;
		$secondmonth=4849;
		$thirdmonth+=852;
		$thirdmonth-=$bak;
	}
}else if($address=="陕西奥诚汽车销售服务有限公司"){
	$time=date('Ym',time());
	if($time=='201607'){
		$firstjk=509;
		$secondjk=693;
	}

	$shijian=date('Y',time());
	if($shijian=='2016'){
		$firstmonth=3421;
		$secondmonth=6797;
		$thirdmonth+=1202;
		$thirdmonth-=$bak;
	}
}


require(ROOT . 'view/iwocar5/4S_SaleDirector.html');
 ?>